package org.softmed.rest.admin.module;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import main.RESTEditor;

import org.softmed.rest.admin.DetailLevelListener;
import org.softmed.rest.admin.DetailChooser.DetailLevel;
import org.softmed.rest.admin.interfaces.CreateListener;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.PathChangedListener;
import org.softmed.rest.admin.interfaces.RemoveListener;
import org.softmed.rest.admin.interfaces.SelectionPathChanged;

import restlet.description.convert.RESTModuleDescription;
import restlet.description.convert.RESTResourceDescription;


public class ModuleEditor extends JPanel implements EventListener,
		RemoveListener, FileHandler, SelectionPathChanged, CreateListener,
		DetailLevelListener {

	RESTModuleDescription moduleDescription;

	ModuleHeader moduleHeader = new ModuleHeader();
	ModuleActionPanel actionPanel = new ModuleActionPanel();
	ModuleResourceManager moduleResourceManager = new ModuleResourceManager();
	EventListener removeModuleListener;

	private FileHandler fileHandler;

	public ModuleEditor() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
//		setBackground(Color.BLACK);
		setMinimumSize(new Dimension(100, 25));

		// setBorder(BorderFactory.createLineBorder(Color.RED));
		// setBackground(Color.YELLOW);

		moduleResourceManager.setFileHandler(fileHandler);

		actionPanel.setAddResourceListener(this);
		moduleHeader.setRemoveListener(this);
		actionPanel.setFileHandler(this);
		actionPanel.setCreateListener(this);
		actionPanel.setDetailListener(this);
		moduleHeader.setPathChangedListener(actionPanel);

		add(moduleHeader);
		add(actionPanel);
		add(moduleResourceManager);

		moduleResourceManager.setEditor(this);

		actionPanel.setDetailLevel(DetailLevel.HANDLER);
		detailLevelChanged(DetailLevel.HANDLER);

	}

	@Override
	public void listen(Object source) {
		moduleResourceManager.addNewResource();
	}

	public RESTModuleDescription getModuleDescription() {
		return moduleDescription;
	}

	public void setModuleDescription(RESTModuleDescription moduleDescription) {
		this.moduleDescription = moduleDescription;
		moduleHeader.setModuleDescription(moduleDescription);
		moduleResourceManager.setResources(moduleDescription.getResources());
	}

	public EventListener getRemoveModuleListener() {
		return removeModuleListener;
	}

	public void setRemoveModuleListener(EventListener removeModuleListener) {
		this.removeModuleListener = removeModuleListener;
	}

	@Override
	public void remove(Object source) {
		removeModuleListener.listen(this);

	}

	public void validateData() throws ValidationException {
		moduleHeader.validateData();
		// moduleResourceManager.validateData();
	}

	public void validateModulesOnly() throws ValidationException {
		moduleHeader.validateData();
	}

	public void selectionPathChanged(boolean directory, String name,
			String parent) {
		actionPanel.selectionPathChanged(directory, name, parent);
		moduleResourceManager.selectionPathChanged(directory, name, parent);

	}

	public void setFileHandler(FileHandler fileHandler) {
		this.fileHandler = fileHandler;
		moduleResourceManager.setFileHandler(fileHandler);
	}

	@Override
	public String retrieveFilePath() {
		String path = fileHandler.retrieveFilePath();
		moduleHeader.path.setText(path);
		return null;
	}

	@Override
	public void created() {
		String path = RESTEditor.RESTEditor.createFileWithPopup();
		moduleHeader.path.setText(path);
	}

	@Override
	public void detailLevelChanged(DetailLevel detailLevel) {
		switch (detailLevel) {
		case MODULE:
			moduleResourceManager.setVisible(false);
			break;

		default:
			moduleResourceManager.setVisible(true);
			moduleResourceManager.setDetailLevel(detailLevel);
			break;
		}

	}

	public DetailLevel getDetailLevel() {
		// TODO Auto-generated method stub
		return actionPanel.detailChooser.getDetailLevel();
	}

	public void setDetailLevel(DetailLevel detailLevel) {
		detailLevelChanged(detailLevel);
		actionPanel.setDetailLevel(detailLevel);

	}
}
